package xyz.eden.cloud.common.web.support.infrastructure.publish;

import org.springframework.context.ApplicationEventPublisher;
import xyz.eden.cloud.common.web.support.infrastructure.publish.enums.EventTypeEnum;
import xyz.eden.cloud.common.web.support.infrastructure.publish.event.ModelBatchChangeEvent;
import xyz.eden.cloud.common.web.support.infrastructure.publish.event.ModelChangeEvent;

import java.util.Collection;

public interface ModelChangeEventPublisher<T> {

    default void onCreated(final T data) {
        this.onEventPublisher(new ModelChangeEvent(data, EventTypeEnum.CREATE, null));
    }

    default void onCreated(final Collection<T> dataList) {
        this.onEventPublisher(new ModelBatchChangeEvent(dataList, EventTypeEnum.CREATE, null));
    }

    default void onUpdated(final T data, final T beforeData) {
        this.onEventPublisher(new ModelChangeEvent(data, EventTypeEnum.UPDATE, beforeData));
    }

    default void onUpdated(final Collection<T> dataList, final Collection<T> beforeDataList) {
        this.onEventPublisher(new ModelBatchChangeEvent(dataList, EventTypeEnum.UPDATE, beforeDataList));
    }

    default void onDeleted(final T data) {
        this.onEventPublisher(new ModelChangeEvent(data, EventTypeEnum.DELETE, data));
    }

    default void onDeleted(final Collection<T> dataList) {
        this.onEventPublisher(new ModelBatchChangeEvent(dataList, EventTypeEnum.DELETE, dataList));
    }


    default void onEventPublisher(ModelChangeEvent event) {
        getEventPublisher().publishEvent(event);
    }

    ApplicationEventPublisher getEventPublisher();
}
